//
//  BKKongZhiXianViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/6.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKKongZhiXianViewController.h"
#import "BKButton.h"
#import "BKFourView.h"
#import "BKResultOfKongZhiXianViewController.h"
#import "BKKongZhiXianViewModel.h"
#import "BKSearchField.h"
#import <MBProgressHUD.h>

@interface BKKongZhiXianViewController ()
@property (weak, nonatomic) IBOutlet BKButton *wen;
@property (weak, nonatomic) IBOutlet BKButton *li;
@property (weak, nonatomic) IBOutlet BKButton *k2015;
@property (weak, nonatomic) IBOutlet BKButton *k2014;
@property (weak, nonatomic) IBOutlet BKButton *k2013;
@property (weak, nonatomic) IBOutlet BKButton *k2012;
@property (weak, nonatomic) IBOutlet BKFourView *yearView;
@property (weak, nonatomic) IBOutlet BKSearchField *dq;

@end

@implementation BKKongZhiXianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _wen.selected  = YES;
    _k2015.selected = YES;
 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)p_back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)p_submit:(id)sender {
    
    if ([_dq.text length]==0 || [_dq.text isEqualToString:@"不限"]) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择考区" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];

        return;
    }
    
    NSString *wl ;
    if (_wen.selected) {
        wl = @"1";
        
    }else{
    
        wl = @"2";
    }
    
    NSString *year;
    if (_k2015.selected) {
        year = @"2015";
        
    }else if (_k2014.selected){
    
        year = @"2014";
    }else if (_k2013.selected){
    
        year = @"2013";
    }else{
    
        year = @"2012";
    }
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.labelText = @"正在加载";
    hud.mode = MBProgressHUDModeIndeterminate;
    [hud show:YES];
    [BKKongZhiXianViewModel kongzhixianWithdq:_dq.text wl:wl year:year success:^(NSArray *dataArray) {
        [hud hide:YES];
        BKResultOfKongZhiXianViewController *result = [[BKResultOfKongZhiXianViewController alloc] initWithNibName:NSStringFromClass([BKResultOfKongZhiXianViewController class]) bundle:nil];
        result.year = year;
        result.dataArray = dataArray;
        result.shengqu = _dq.text;
        
        [self.navigationController pushViewController:result animated:YES];
    } failure:^(NSString *message) {
        dispatch_async(dispatch_get_main_queue(), ^{
           
            
            [hud hide:YES];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
            [alertView show];
        });

    }];
    
  
    
    
}
- (IBAction)p_wenli:(id)sender {
    if ([sender isEqual:_wen]) {
        _wen.selected = YES;
        _li.selected = NO;
    }else{
    
        _li.selected = YES;
        _wen.selected = NO;
    }
}
- (IBAction)p_year:(id)sender {
    
    for (id view in [_yearView subviews]) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)view;
            
            if ([view isEqual:sender]) {
                button.selected = YES;
            }else{
            
                button.selected = NO;
               
            }
        }
    }
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
